package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.model.settlement.OrderSourceCost;
import com.tiancheng.trade.settlement.service.settlement.IOrderSourceCostService;
import com.tiancheng.trade.settlement.vo.settlement.api.GetOrderSourceCostVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 收入成本报表
 *
 * @author lzh
 */
@RestController
@RequestMapping("/admin/sourcecost")
@Validated
@Slf4j
public class OrderSourceCostController {

    @Autowired
    IOrderSourceCostService sourceCostService;

    /**
     * 报表类型 1酒店 3门票 4线路
     * 查询收入成本报表信息
     *
     * @param orderSource
     * @return
     */
    @GetMapping("/findPageList")
    public Result findPageList(GetOrderSourceCostVO orderSource) throws BusinessException {

        PageInfo<OrderSourceCost> list = sourceCostService.getOrderSourceCostList(orderSource);
        if (null == list || CollectionUtils.isEmpty(list.getList())) {
            return Result.success(0);
        }
        return Result.success(list);
    }

    /**
     * 报表类型 1酒店 3门票 4线路
     * 查询收入成本报表信息
     *
     * @param orderSource
     * @return
     */
    @GetMapping("/findList")
    public Result findList(GetOrderSourceCostVO orderSource) throws BusinessException {

        PageInfo<OrderSourceCost> list = sourceCostService.getOrderSourceCostList(orderSource);
        if (CollectionUtils.isEmpty(list.getList())) {
            return Result.success(0);
        }
        return Result.success(list);
    }

    /**
     * 报表类型 1酒店 3门票 4线路
     * 更新收入成本报表信息
     *
     * @param date
     * @return
     */
    @GetMapping("/add")
    public Result add(String date, @RequestParam(name = "order_source") String orderSource) throws BusinessException {
        int data = sourceCostService.addOrderSourceCost(date, orderSource);
        return Result.success("操作成功");
    }

    /**
     * 报表类型 1酒店 3门票 4线路
     * 更新收入成本报表信息
     *
     * @param date
     * @return
     */
    @GetMapping("/update")
    public Result update(String date, @RequestParam(name = "order_source") String orderSource) throws BusinessException {
        int data = sourceCostService.updateOrderSourceCost(date, orderSource);
        return Result.success("操作成功");
    }

    /**
     * 场景收入成本报表
     *
     * @param orderSourceCostVO
     * @return
     */
    @PostMapping("/export")
    public Result orderSourceCostExport(@RequestBody GetOrderSourceCostVO orderSourceCostVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        sourceCostService.export(orderSourceCostVO, request, response);
        return Result.success("导出成功");
    }

}
